package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blog.common.constant.Constants;
import com.blog.common.exception.BlogException;
import com.blog.common.util.BeanTransUtils;
import com.blog.common.util.JwtUtils;
import com.blog.common.util.Md5Utils;
import com.blog.mapper.UserInfoMapper;
import com.blog.pojo.dataobject.BlogInfo;
import com.blog.pojo.dataobject.UserInfo;
import com.blog.pojo.request.UserLoginRequest;
import com.blog.pojo.response.UserInfoResponse;
import com.blog.pojo.response.UserLoginResponse;
import com.blog.service.BlogService;
import com.blog.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource(name = "userInfoMapper")
    private UserInfoMapper userInfoMapper;

    @Resource(name = "blogServiceImpl")
    private BlogService blogService;

    @Override
    public UserLoginResponse check(UserLoginRequest loginRequest) {
        /**
         * 1. 判断密码是否正确
         * 2. 如果正确, 返回token
         */
        UserInfo userInfo = selectUserInfoByName(loginRequest.getUserName());
        if (userInfo==null){
            //用户不存在
            throw new BlogException("用户不存在");
        }
//        if (!loginRequest.getPassword().equals(userInfo.getPassword())){
//            //密码不正确
//            throw new BlogException("密码不正确");
//        }
        if (!Md5Utils.verify(loginRequest.getPassword(), userInfo.getPassword())){
            //密码不正确
            throw new BlogException("密码不正确");
        }
        //密码正确
        Map<String, Object> map = new HashMap<>();
        map.put("id", userInfo.getId());
        map.put("name", userInfo.getUserName());
        String token = JwtUtils.genJwt(map);
        return new UserLoginResponse(userInfo.getId(), token);
    }

    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        return BeanTransUtils.transUser(selectUserInfoById(userId));
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        BlogInfo blogInfo = blogService.getBlogInfo(blogId);
        if (blogInfo==null){
            throw new BlogException("博客不存在");
        }
        return BeanTransUtils.transUser(selectUserInfoById(blogInfo.getUserId()));
    }

    public UserInfo selectUserInfoByName(String userName){
        return userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getUserName, userName)
                        .eq(UserInfo::getDeleteFlag, Constants.NOT_DELETE)
        );
    }

    public UserInfo selectUserInfoById(Integer userId){
        return userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getId, userId)
                        .eq(UserInfo::getDeleteFlag, Constants.NOT_DELETE)
        );
    }
}
